<template>
    <el-dialog :title="dialogTitle" width="70%" @close="handleClose">
        <el-form ref="formRef" :model="form"  label-width="120px">
            <!-- 第一行 -->
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="所有者" prop="owner">
                        <el-select v-model="form.owner" placeholder="请选择所有者" clearable size="large">
                            <el-option v-for="item in ownerList" :key="item.id" :label="item.name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="公司" prop="company">
                        <el-input v-model="form.company" placeholder="请输入公司名称" />
                    </el-form-item>
                </el-col>
            </el-row>

            <!-- 第二行 -->
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="称呼" prop="appellation">
                        <el-select v-model="form.appellation" placeholder="选择称呼" clearable size="large">
                            <el-option v-for="item in appellationList" :key="item.id" :label="item.value"
                                :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="姓名" prop="fullname">
                        <el-input v-model="form.fullname" placeholder="请输入姓名" />
                    </el-form-item>
                </el-col>
            </el-row>

            <!-- 第三行 -->
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="职位" prop="job">
                        <el-input v-model="form.job" placeholder="请输入职位" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="form.email" placeholder="请输入邮箱" />
                    </el-form-item>
                </el-col>
            </el-row>

            <!-- 第四行 -->
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="公司座机" prop="mphone">
                        <el-input v-model="form.mphone" placeholder="请输入公司座机" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="手机" prop="phone">
                        <el-input v-model="form.phone" placeholder="请输入手机号码" />
                    </el-form-item>
                </el-col>
            </el-row>

            <!-- 第五行 -->
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="线索来源" prop="source">
                        <el-select v-model="form.source" placeholder="选择线索来源" clearable size="large">
                            <el-option v-for="item in sourceList" :key="item.id" :label="item.value" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="线索状态" prop="state">
                        <el-select v-model="form.state" placeholder="选择线索状态" clearable size="large">
                            <el-option v-for="item in stateList" :key="item.id" :label="item.value" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <!-- 第六行 -->
            <el-row :gutter="20">
                <el-col :span="24">
                    <el-form-item label="线索描述" prop="description">
                        <el-input type="textarea" v-model="form.description" placeholder="请输入线索描述" />
                    </el-form-item>
                </el-col>
            </el-row>

            <!-- 第七行 -->
            <el-row :gutter="20">
                <el-col :span="24">
                    <el-form-item label="联系纪要" prop="contactSummary">
                        <el-input type="textarea" v-model="form.contactSummary" placeholder="请输入联系纪要" />
                    </el-form-item>
                </el-col>
            </el-row>

            <!-- 第八行 -->
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="下次联系时间" prop="nextContactTime">
                        <el-date-picker value-format="YYYY-MM-DD" v-model="form.nextContactTime" type="datetime" placeholder="选择下次联系时间" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="详细地址" prop="address">
                        <el-input type="textarea" v-model="form.address" placeholder="请输入详细地址" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button type="primary" @click="handleConfirm">确认</el-button>
                <el-button @click="handleClose">取消</el-button>
            </span>
        </template>
    </el-dialog>

</template>

<script setup>
import { defineEmits, defineProps, ref, watch } from "vue"
import requestUtil from "@/util/request";
import { ElMessage } from 'element-plus'

const props = defineProps(
    {
        id: {
            type: String,
            default: "",
            required: true
        },
        dialogTitle: {
            type: String,
            default: '',
            required: true
        },
        dialogVisible: {
            type: Boolean,
            default: false,
            required: true
        },
        sourceList: {
            type: Array,
            default: false,
            required: true
        },
        stateList: {
            type: Array,
            default: false,
            required: true
        },
        ownerList: {
            type: Array,
            default: false,
            required: true
        },
        appellationList: {
            type: Array,
            default: false,
            required: true
        }
    }
)
//表单数据
const form = ref({
    'id': '',
    'fullname': '',
    'appellation': '',
    'owner': '',
    'company': '',
    'job': '',
    'email': '',
    'phone': '',
    'website': '',
    'mphone': '',
    'state': '',
    'source': '',
    'description': '',
    'contactSummary': '',
    'nextContactTime': '',
    'address': ''

})

const formRef = ref(null)

const initFormData = async () => {
    const res = await requestUtil.getRestFulApi("crmClue/getClueInfo/{id}", { id: props.id }, "");
    form.value = res.data.data;
}

watch(
    () => props.dialogVisible,
    () => {
        let id = props.id;
        if (id != "" && props.dialogVisible) {
            initFormData(id);
        } else {
            form.value = {
                id: "",
                owner: "",
                name: "",
                startDate: "",
                endDate: "",
                cost: "",
                description: "",
            }
        }
    }
)

const emits = defineEmits(['update:modelValue', 'initClueList'])
//关闭from表单
const handleClose = () => {
    emits('update:modelValue', false)
}

const handleConfirm = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            let data;
            console.log(form.value.id, Boolean(form.value.id));

            if (Boolean(form.value.id)) {
                let result = await requestUtil.post("crmClue/update", form.value);
                data = result.data;
            } else {
                let result = await requestUtil.post("crmClue/saveCreateClue", form.value);
                data = result.data;
            }
            if (data.code == 1) {
                ElMessage.success("执行成功！")
                formRef.value.resetFields();
                form.value = {};
                emits("initClueList")
                handleClose();
            } else {
                ElMessage.error(data.message);
            }
        } else {
            ElMessage.error(fail);

        }
    })
}

</script>

<style lang="scss" scoped></style>